#!/usr/bin/env bash

# 项目目录
PROJECT_DIR=/python_project
cd $PROJECT_DIR
chmod 777 -R $PROJECT_DIR

rm -rf /etc/apt/sources.list.d/debian.sources
# 创建Debian源配置
echo 'Types: deb
URIs: http://mirrors.tuna.tsinghua.edu.cn/debian
Suites: bookworm bookworm-updates bookworm-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
Types: deb-src
URIs: http://mirrors.tuna.tsinghua.edu.cn/debian
Suites: bookworm bookworm-updates bookworm-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# 以下安全更新软件源包含了官方源与镜像站配置，如有需要可自行修改注释切换
Types: deb
URIs: http://mirrors.tuna.tsinghua.edu.cn/debian-security
Suites: bookworm-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

Types: deb-src
URIs: http://mirrors.tuna.tsinghua.edu.cn/debian-security
Suites: bookworm-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# Types: deb
# URIs: http://security.debian.org/debian-security
# Suites: bookworm-security
# Components: main contrib non-free non-free-firmware
# Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# Types: deb-src
# URIs: http://security.debian.org/debian-security
# Suites: bookworm-security
# Components: main contrib non-free non-free-firmware
# Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
' | tee /etc/apt/sources.list.d/debian.sources

# 更新apt
apt update

# 安装nginx并自动选择选项
echo -e "5\n69\n" | apt install -y nginx

# 配置nginx
mv python_project.conf /etc/nginx/sites-enabled/python_project.conf
chmod 777 /etc/nginx/sites-enabled/python_project.conf && rm -rf /etc/nginx/sites-enabled/default
service nginx start

# 启动前端服务
cd $PROJECT_DIR/dash-fastapi-frontend
nohup python wsgi.py --env=prod & > frontend.log

# 启动后端服务
cd $PROJECT_DIR/dash-fastapi-backend
nohup python app.py --env=prod & > backend.log

# 查看nginx访问日志
tail -F /var/log/nginx/access.log